package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crplatformenhance.service.CrPlatformEnhanceService;
import com.epoch.app.crpricecenter.model.dto.CreditInfoSDO;
import com.epoch.app.crpricecenter.model.dto.CreditQueryRequest;
import com.epoch.app.crpricecenter.model.dto.QueryCreditListResponse;
import com.epoch.app.crpricecenter.service.CrPriceCenterService;
import com.epoch.app.otsapplicationappsdk.customdetail.dto.AsyncRequest;
import com.epoch.app.otsapplicationappsdk.customdetail.dto.RenderRequest;
import com.epoch.app.otsapplicationappsdk.customdetail.service.CustomDetailService;
import com.epoch.app.crplatformenhance.model.dto.PageQueryAddressListRequest;
import com.epoch.app.crplatformenhance.model.dto.PageQueryAddressListResponse;
import com.epoch.app.crplatformenhance.model.dto.QueryCustomerInfoRequest;
import com.epoch.app.crplatformenhance.model.dto.QueryCustomerInfoResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class CustomDetailV2ServiceImpl implements CustomDetailService {

    private static Log log = Log.getLogger(CustomDetailV2ServiceImpl.class);

//    @Autowired
//    private OtsPlatformEnhanceService otsPlatformEnhanceService;
    @Autowired
    private CrPlatformEnhanceService crPlatformEnhanceService;
    @Autowired
    private CrPriceCenterService crPriceCenterService;

    @Override
    public Object render(RenderRequest renderRequest) {
        log.info("客户详情渲染页面请求参数 {} ", JSONObject.toJSONString(renderRequest));

        JSONObject result = new JSONObject();
        int limit = 10;
        if (renderRequest.getPageSize() != null && renderRequest.getPageSize() > 0) {
            limit = renderRequest.getPageSize();
        }
        if (renderRequest.getCurrentPage() == null) {
            renderRequest.setCurrentPage(1);
        }
        try {
            PageQueryAddressListRequest addressListRequest = new PageQueryAddressListRequest();
            BeanUtils.copyProperties(renderRequest, addressListRequest);
            addressListRequest.setCustomerCode(renderRequest.getCustomerCode());
            addressListRequest.setPage(renderRequest.getCurrentPage());
            addressListRequest.setSize(limit);
            PageQueryAddressListResponse addressListResponse = crPlatformEnhanceService.pageQueryAddressList(addressListRequest);
            result.put("addressListResponse", addressListResponse.getResult());

            QueryCustomerInfoRequest customerInfoRequest = new QueryCustomerInfoRequest();
            customerInfoRequest.setCode(renderRequest.getCustomerCode());
            QueryCustomerInfoResponse customerInfoResponse = crPlatformEnhanceService.queryCustomerInfo(customerInfoRequest);
            result.put("customerInfoResponse", customerInfoResponse.getResult());

            //客户信贷余额
            CreditQueryRequest creditQueryRequest = new CreditQueryRequest();
            creditQueryRequest.setOrganizationCode(renderRequest.getOrgSaleOrganizationCode());
            creditQueryRequest.setChannelCode(renderRequest.getChannelCode());
            creditQueryRequest.setCustomerCode(renderRequest.getCustomerCode());
            creditQueryRequest.setItemGroupCode(renderRequest.getItemGroup());
            QueryCreditListResponse queryCreditListResponse = crPriceCenterService.queryCreditList(creditQueryRequest);
            if (Objects.nonNull(queryCreditListResponse)&& CollectionUtils.isNotEmpty(queryCreditListResponse.getResult())){
                CreditInfoSDO creditInfoSDO = queryCreditListResponse.getResult().get(0);
                result.put("creditBalanceText", creditInfoSDO.getBalance());
                result.put("feeBalanceText", creditInfoSDO.getFeeBalance());
            }


            int totalPage = addressListResponse.getTotal() / limit;
            if (addressListResponse.getTotal() % limit > 0) {
                totalPage++;
            }
            result.put("totalPage", totalPage);

            result.put("success", true);
            result.put("message", "");

        } catch (BeansException e) {
            result.put("success", false);
            result.put("message", "页面跑丢了");
        }
        return result;
    }

    @Override
    public Object async(AsyncRequest asyncRequest) {
        log.info("客户详情异步 请求参数 {} ", JSON.toJSONString(asyncRequest));

        Object request = asyncRequest.getRequest();
        if (Objects.isNull(request)) {
            return null;
        }
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(request));
            JSONObject fields = jsonObject.getJSONObject("fields");
            String customerCode = fields.getString("customerCode");
            Integer currentPage = fields.getInteger("currentPage");
            String creditBalanceText = fields.getString("creditBalanceText");
            String feeBalanceText = fields.getString("feeBalanceText");
            result.put("creditBalanceText",creditBalanceText);
            result.put("feeBalanceText",feeBalanceText);
            Integer pageSize = fields.getInteger("pageSize");
            int limit = 10;
            if (pageSize != null && pageSize > 0) {
                limit = pageSize;
            }
            if (currentPage == null) {
                currentPage = 1;
            }
            QueryCustomerInfoRequest customerInfoRequest = new QueryCustomerInfoRequest();
            customerInfoRequest.setCode(customerCode);
            QueryCustomerInfoResponse customerInfoResponse = crPlatformEnhanceService.queryCustomerInfo(customerInfoRequest);
            result.put("customerInfoResponse", customerInfoResponse.getResult());

            PageQueryAddressListRequest addressListRequest = new PageQueryAddressListRequest();
            addressListRequest.setCustomerCode(customerCode);
            addressListRequest.setPage(currentPage);
            addressListRequest.setSize(limit);
            PageQueryAddressListResponse addressListResponse = crPlatformEnhanceService.pageQueryAddressList(addressListRequest);
            if (addressListResponse == null) {
                throw new Exception("未查到结果");
            }
            result.put("success", addressListResponse.getSuccess());
            result.put("addressListResponse", addressListResponse.getResult());
            result.put("pageSize", limit);
            result.put("currentPage", currentPage);
            result.put("total", addressListResponse.getTotal());
            int totalPage = addressListResponse.getTotal() / limit;
            if (addressListResponse.getTotal() % pageSize > 0) {
                totalPage++;
            }
            result.put("totalPage", totalPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "页面跑丢了");
        }
        return result;
    }

}
